package com.tsingteng.cosfun.ui.message.fans;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BasePresenterFragment;
import com.tsingteng.cosfun.bean.FansNotificationBean;
import com.tsingteng.cosfun.bean.FollowBean;
import com.tsingteng.cosfun.helper.VideoHelper;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.ui.mine.TerminalActivity;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.SPUtils;
import com.tsingteng.cosfun.utils.VideoUtils;
import com.tsingteng.cosfun.widget.MyRecycleView;
import com.tsingteng.cosfun.widget.TitleView;

import butterknife.BindView;

/**
 * 描述 :粉丝页面
 * 创建人: 肖利.
 * 时间:2018/8/7
 * qq(872179966)
 */

public class FansFragment extends BasePresenterFragment<FansPresenter, FansContract.IFansView> implements FansContract.IFansView, FansAdapter.OnClickFollowTypeListener {


    @BindView(R.id.rv_fan_rv)
    MyRecycleView rvFanRv;
    @BindView(R.id.tv_view)
    TitleView titleView;
    private String funType = "FANS";
    private int page = 1;
    private TextView followType;
    private FansNotificationBean.ListBean item;

    public static FansFragment newInstance() {
        FansFragment fragment = new FansFragment();
        return fragment;
    }

    @Override
    protected void refresh() {
        page = 1;
        mPresenter.loginFansNotification(funType, page);
    }

    @Override
    protected void initData(View rootView) {
        AppUtils.setMessageNumber("FANS", 0);
        titleView.setIvLeft(R.drawable.back_big_icon);
        titleView.setTitle("粉丝");
        mPresenter.loginFansNotification(funType, page);
        titleView.setOnTitleClickListener(new TitleView.OnTitleClickListener() {
            @Override
            public void onLeft() {
                getActivity().finish();
            }

            @Override
            public void onRight() {

            }

            @Override
            public void onRight2() {

            }
        });

        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_null_layout, null);
        view.findViewById(R.id.btn_start_shot).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                VideoHelper.getIntence().starVideoShoot(getActivity());
            }
        });
        mAdapter.setEmptyView(view);
    }

    @Override
    protected FansPresenter createPresenter() {
        return new FansPresenter();
    }

    @Override
    protected View initHeader() {
        return null;
    }

    @Override
    protected MyRecycleView createRecycleView() {
        return rvFanRv;
    }

    @Override
    protected BaseQuickAdapter createAdapter() {
        FansAdapter fansAdapter = new FansAdapter(getFansPresenter());
        fansAdapter.setOnClickFollowTypeListener(this);
        return fansAdapter;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        FansNotificationBean.ListBean listBean = (FansNotificationBean.ListBean) adapter.getItem(position);
        int tag = 1;
        if (listBean.getSource().getId() != SPUtils.init().getInt("profile_id")) {
            Bundle bundle = new Bundle();
            bundle.putInt("tag", tag);
            bundle.putInt("user_id", listBean.getSource().getId());
            bundle.putInt("workId", listBean.getSource().getId());
            TerminalActivity.showFragment(getContext(), HomeMineFragment.class, bundle);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("tag", tag);
            bundle.putInt("user_id", listBean.getTarget().getId());
            bundle.putInt("workId", listBean.getTarget().getId());
            TerminalActivity.showFragment(getContext(), HomeMineFragment.class, bundle);
        }

        ImageView iv_fans_isread = view.findViewById(R.id.iv_fans_isread);
        iv_fans_isread.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        mPresenter.loginFansNotification(funType, page);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_fans;
    }

    @Override
    public void getFollowResult(FollowBean followBean) {
        if (followBean.getRelationType()<=0) {
            followType.setText("关注");
            followType.setBackground(getActivity().getResources().getDrawable(R.drawable.shape_bg_white));
            followType.setTextColor(getActivity().getResources().getColor(R.color.black));
        } else if (followBean.getRelationType()==1) {
            followType.setText("已关注");
            followType.setBackground(getActivity().getResources().getDrawable(R.drawable.shape_trank_bg));
            followType.setTextColor(getActivity().getResources().getColor(R.color.white));
        } else if (followBean.getRelationType()==2) {
            followType.setText("相互关注");
            followType.setBackground(getActivity().getResources().getDrawable(R.drawable.shape_pink_bg));
            followType.setTextColor(getActivity().getResources().getColor(R.color.color_ffc1ee));
        }
        item.setFollowType(followBean.getRelationType());

    }

    @Override
    public void getNoFollowResult(FollowBean followBean) {
        if (followBean.getRelationType()<=0) {
            followType.setText("关注");
            followType.setBackground(getActivity().getResources().getDrawable(R.drawable.shape_bg_white));
            followType.setTextColor(getActivity().getResources().getColor(R.color.black));
        } else if (followBean.getRelationType()==1) {
            followType.setText("已关注");
            followType.setBackground(getActivity().getResources().getDrawable(R.drawable.shape_trank_bg));
            followType.setTextColor(getActivity().getResources().getColor(R.color.white));
        } else if (followBean.getRelationType()==2) {

            followType.setText("相互关注");
            followType.setBackground(getActivity().getResources().getDrawable(R.drawable.shape_pink_bg));
            followType.setTextColor(getActivity().getResources().getColor(R.color.color_ffc1ee));
        }
        item.setFollowType(followBean.getRelationType());
    }

    @Override
    public void getShowFansData(FansNotificationBean notificationBean) {
        if (rvFanRv != null) rvFanRv.refreshComplete();
        if (notificationBean == null || notificationBean.getList() == null || notificationBean.getList().size() == 0)
            return;
        mAdapter.addData(notificationBean.getList());
        mAdapter.notifyDataSetChanged();
        if (notificationBean.isHasNextPage()) {
            mAdapter.loadMoreComplete();
        } else {
            mAdapter.loadMoreEnd();
        }
        if (page == 1) {
            mPresenter.loginReadNotice(notificationBean.getList().get(0).getId());
        }

    }

    @Override
    public void showReadResult(Integer integer) {
        Log.e("isRead", integer + "");
    }


    public FansPresenter getFansPresenter() {
        return mPresenter;
    }

    @Override
    public void onClickFolllowType(FansNotificationBean.ListBean item, TextView textView) {
        this.followType = textView;
        this.item=item;
        if (item.getFollowType() == 0) {
            mPresenter.loginFollowFans(item.getSource().getId() + "");
        } else if (item.getFollowType() == 1) {
            mPresenter.loginNoFollow(item.getSource().getId() + "");
        } else if (item.getFollowType() == 2) {
            mPresenter.loginNoFollow(item.getSource().getId() + "");
        }
    }
}
